﻿AB.namespace("INV_OrderListGeLi.Mgr");
INV_OrderListGeLi.Mgr = {
    commandHandler: function (command, record) {
        var FId = record.data.FId;
        switch (command) {
            case "ViewOrder": // 查看送货单
                EditOrderFIdH.setValue(FId);
                EditOrderStore.load(
                { callback: function (records, options, success) {
                    if (success) {
                        OrderStatus.setValue(records[0].data.FStatus);
                        if (OrderStatus.getValue() == "Approve" || OrderStatus.getValue() == "Delete") {
                            btSaveOrder.setDisabled(true);
                            FCustomerMobile.setDisabled(true);
                            FCustomerPhone.setDisabled(true);
                            FCustomerName.setDisabled(true);
                            FCustomerAddress.setDisabled(true);
                            FRemark.setDisabled(true);
                            FStatus.setDisabled(true);
                            btPrint.setDisabled(false);
                            if (OrderStatus.getValue() == "Delete") {
                                btPrint.setDisabled(true);
                            }
                        } else {
                            if (OrderStatus.getValue() == "Save") {
                                btPrint.setDisabled(true);
                                btSaveOrder.setDisabled(false);
                                FCustomerMobile.setDisabled(false);
                                FCustomerPhone.setDisabled(false);
                                FCustomerName.setDisabled(false);
                                FCustomerAddress.setDisabled(false);
                                FRemark.setDisabled(false);
                                FStatus.setDisabled(false);
                            }
                        }
                    }
                }
                }
                );
                SelectOrderFIdH.setValue(FId);
                OrderDetailListStore.load();

                break;
            case "EditOrder": // 编辑送货单
                EditOrderFIdH.setValue(FId);
                EditOrderStore.load(
                { callback: function (records, options, success) {
                    if (success) {
                        OrderStatus.setValue(records[0].data.FStatus);
                        if (OrderStatus.getValue() == "Approve" || OrderStatus.getValue() == "Delete") {
                            btSaveOrder.setDisabled(true);
                            FCustomer.setDisabled(true);
                            FRemark.setDisabled(true);
                            FStatus.setDisabled(true);
                            btPrint.setDisabled(false);
                            if (OrderStatus.getValue() == "Delete") {
                                btPrint.setDisabled(true);
                            }
                        } else {
                            if (OrderStatus.getValue() == "Save") {
                                btPrint.setDisabled(true);
                                btSaveOrder.setDisabled(false);
                                FCustomer.setDisabled(false);
                                FRemark.setDisabled(false);
                                FStatus.setDisabled(false);
                            }
                        }
                    }
                }
                });
                SelectOrderFIdH.setValue(FId);
                OrderDetailListStore.load();

                break;
            case "DelOrder": // 删除送货单
                SelectOrderFIdH.setValue(FId);
                Ext.Msg.confirm('提示', '确定要删除该记录吗?', function (btn) {
                    if (btn == "yes") {
                        Ext.Ajax.request({
                            url: "/SendOrder/delOrder",
                            method: 'post',
                            params: {
                                FId: FId
                            },
                            success: function (result) {
                                if (!isTimeOut(result)) {
                                    var response = Ext.util.JSON.decode(result.responseText);
                                    if (response.success == false) {
                                        ajaxFailureHandler(response);
                                    } else {
                                        if (response.result == true) {
                                            SelectOrderFIdH.setValue("");
                                            alert("删除送货单成功");
                                            OrderListStore.reload();
                                            var editOrderFormPanel = Ext.getCmp("EditOrderFormPanel");
                                            editOrderFormPanel.reset();
                                            btSaveOrder.setDisabled(true);
                                            FCustomer.setDisabled(true);
                                            FRemark.setDisabled(true);
                                            FStatus.setDisabled(true);
                                            btPrint.setDisabled(true);
                                            OrderDetailListStore.removeAll();
                                        } else {
                                            alert(response.errorMessage);
                                        }
                                    }
                                }
                            }
                        });
                    }
                });
                break;
            case "SelectOrder": // 选择送货单
                SelectOrderFIdH.setValue(FId);
                OrderDetailListStore.load();
                break;
            case "DelOrderDetail": // 删除送货单详情
                SelectOrderDetailFIdH.setValue(FId);
                Ext.Msg.confirm('提示', '确定要删除该记录吗?', function (btn) {
                    if (btn == "yes") {
                        Ext.Ajax.request({
                            url: "/SendOrder/delOrderDetail",
                            method: 'post',
                            params: {
                                FId: FId
                            },
                            success: function (result) {
                                if (!isTimeOut(result)) {
                                    var response = Ext.util.JSON.decode(result.responseText);
                                    if (response.success == false) {
                                        ajaxFailureHandler(response);
                                    } else {
                                        SelectOrderDetailFIdH.setValue("");
                                        alert("删除送货单详情成功");
                                        OrderDetailListStore.reload();
                                    }
                                }
                            }
                        });
                    }
                });
                break;
            default:
                break;
        }
    },
    // 选择送货单
    SelectOrder: function (e) {
        var record = OrderListGridPanel.getSelectionModel().getSelected();
        var FId = record.data.FId;
        SelectOrderFIdH.setValue(FId);
        OrderDetailListStore.load();
    },
    // 搜索
    Search: function () {
        OrderListStore.load();
    },
    // 清空
    Clear: function () {
        SearchFormPanel.reset();
    },
    // 新增送货单
    NewOrder: function () {
        EditOrderFIdH.setValue("");
        EditOrderFormPanel.reset();
    },
    // 保存送货单
    SaveOrder: function () {
        var FId = EditOrderFIdH.getValue();
        if ("" == FId || "undefied" == FId) {
            Ext.Msg.show({
                title: '操作失败',
                msg: "请先选择要修改的送货单！",
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }
        EditOrderFormPanel.form.submit({
            waitMsg: '正在保存送货单',
            params: {
                FId: FId
            },
            success: function (form, action) {
                alert(action.result.extraParams.msg);
                EditOrderFIdH.setValue(action.result.extraParams.FId);
                OrderListStore.load();
                EditOrderStore.load({ callback: function (records, options, success) {
                    if (success) {
                        OrderStatus.setValue(records[0].data.FStatus);
                        if (OrderStatus.getValue() == "Approve" || OrderStatus.getValue() == "Delete") {
                            btSaveOrder.setDisabled(true);
                            FCustomer.setDisabled(true);
                            FRemark.setDisabled(true);
                            FStatus.setDisabled(true);
                            btPrint.setDisabled(false);
                            if (OrderStatus.getValue() == "Delete") {
                                btPrint.setDisabled(true);
                            }
                        } else {
                            if (OrderStatus.getValue() == "Save") {
                                btPrint.setDisabled(true);
                                btSaveOrder.setDisabled(false);
                                FCustomer.setDisabled(false);
                                FRemark.setDisabled(false);
                                FStatus.setDisabled(false);
                            }
                        }
                    }
                }
                });
            },
            failure: function (form, action) {
                Ext.Msg.show({
                    title: '操作失败',
                    msg: action.result.extraParams.msg,
                    buttons: Ext.Msg.OK,
                    icon: Ext.Msg.ERROR
                });
            }
        });
    },
    formStoreLoaded: function (store, records) {
        var formObj = Ext.getCmp("EditOrderFormPanel");
        formStoreLoaded(store, records, formObj);
        OrderStatusListForSaveStore.reload();
    },
    Print: function () {
        var FId = SelectOrderFIdH.getValue();
        var SortDirectValue = SortDirect.Value;
        if (FId == "") {
            Ext.Msg.show({
                title: '操作失败',
                msg: '请先保存选择发货单!',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }
        if (OrderStatus.getValue() != "Approve") {
            Ext.Msg.show({
                title: '操作失败',
                msg: '只有已审核的发货单才可以导出并打印!',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }
        window.open("/SendOrder/ExportSendOrderToExcelGeLi?FId=" + FId + "&SortDirect=" + SortDirectValue);
    }


}